package com.huwaiwai.lock.reentrantreadwritelock;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by hww on 2018/6/11 下午8:02.
 */
public class Test {

    public static void main(String [] args) {
        testReentrantReadWriteLock();
//        testThreadLocalInit();

    }

    private static void testReentrantReadWriteLock() {
        ReadWriteLock lock = new ReentrantReadWriteLock();
        new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            try {
                System.out.println(threadName + "] 尝试获取读锁 " + System.currentTimeMillis());
                lock.readLock().lockInterruptibly();
                System.out.println(threadName + "] 获取读锁成功 " + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.readLock().unlock();
                System.out.println(threadName + "] 释放读锁成功 " + System.currentTimeMillis());
            }
        }).start();
        new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            try {
                System.out.println(threadName + "] 尝试获取读锁 " + System.currentTimeMillis());
                lock.readLock().lockInterruptibly();
                System.out.println(threadName + "] 获取读锁成功 " + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.readLock().unlock();
                System.out.println(threadName + "] 释放读锁成功 " + System.currentTimeMillis());
            }
        }).start();
        new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            try {
                System.out.println(threadName + "] 尝试获取写锁 " + System.currentTimeMillis());
                lock.writeLock().lockInterruptibly();
                System.out.println(threadName + "] 获取写锁成功 " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.writeLock().unlock();
                System.out.println(threadName + "] 释放写锁成功 " + System.currentTimeMillis());
            }
        }).start();
    }

    public static void testThreadLocalInit() {
        System.out.println("开始创建 ThreadLocalSon");
        ThreadLocalSon t = new ThreadLocalSon();
        System.out.println("创建 ThreadLocalSon 完毕");
        System.out.println("开始提取 ThreadLocal变量");
        String value1 = t.get();
        System.out.println("提取 ThreadLocal变量 完毕");
        System.out.println("value = " + value1);
        t.remove();
        System.out.println("开始提取 ThreadLocal变量");
        String value2 = t.get();
        System.out.println("提取 ThreadLocal变量 完毕");
        System.out.println("value = " + value2);


    }

    static final class ThreadLocalSon extends ThreadLocal<String> {
        public String initialValue() {
            System.out.println("调用了init方法");
            return "huwaiwai";
        }
    }






}
